var _api = _interopRequireDefault(require("../../utils/api")), _toptip = _interopRequireDefault(require("../../public/utils/toptip"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var a = null != arguments[t] ? arguments[t] : {}, i = Object.keys(a);
        "function" == typeof Object.getOwnPropertySymbols && (i = i.concat(Object.getOwnPropertySymbols(a).filter(function(t) {
            return Object.getOwnPropertyDescriptor(a, t).enumerable;
        }))), i.forEach(function(t) {
            _defineProperty(e, t, a[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

var TURNOVER_PLAN = Array.from(Array(10), function(t, e) {
    return {
        id: e + 1,
        name: "".concat(e += 1, "0%")
    };
}), NICHE_PARAMS = {
    type: "客户类型",
    status: "客户状态",
    rate: "预计成交率"
}, NICHE_STATUS = {
    DEFAULT: 0,
    ALREADY: 1,
    ANOTHER: 2
};

Component({
    properties: {
        top: String,
        maxlength: {
            type: Number,
            value: 10
        }
    },
    data: {
        toptip: "",
        isShown: !1,
        client: {
            yetNiche: 2
        },
        form: {}
    },
    methods: {
        toggle: function(t) {
            var e = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
            this.setData({
                isShown: t,
                client: Object.assign({
                    yetNiche: 2
                }, e)
            }), "yetNiche" in e || this.checkClientStatus();
        },
        checkClientStatus: function() {
            var a = this, t = this.$appData.card;
            if (!t.cardid || !t.guid) return Promise.resolve();
            var e = this.data.client.uid;
            _api.default.minicard.business.checkisinbusiness({
                data: {
                    guid: t.guid,
                    cardid: t.cardid,
                    touid: e
                }
            }).then(function(t) {
                var e = t.data.status;
                a.setData({
                    "client.yetNiche": +e
                });
            });
        },
        onClose: function() {
            this.setData({
                isShown: !1
            });
        },
        onInput: function(t) {
            var e = t.detail.value;
            this.remarkLimit(e), this.setData({
                "client.desc": e
            });
        },
        remarkLimit: function(t) {
            var e = this.data, a = e.maxlength, i = e.client.yetNiche;
            if (t.trim().length > a) {
                var n = 1 === i || 2 === i ? "最多可设置10个字符" : "备注过长，建议转为客户后，添加客户详细备注";
                return _toptip.default.show(this, n), !0;
            }
            return !1;
        },
        onSave: function() {
            var t = this, e = this.data.client, a = e.desc, i = e.chatid, n = e.uid;
            this.remarkLimit(a) || _api.default.chat.setting.setchatinfo({
                data: {
                    uid: n,
                    desc: a,
                    chatid: i
                },
                loading: "设置中..."
            }).then(function() {
                t.onClose(), t.triggerEvent("remark", a);
            });
        },
        getLabels: function() {
            var a = this, t = this.$appData.card.guid;
            this.fetchLabel = _api.default.minicard.businesslabel.view({
                data: {
                    guid: t
                }
            }).then(function(t) {
                var e = t.data;
                e.rate = TURNOVER_PLAN, a.setData({
                    nicheLabel: e
                });
            });
        },
        selectRadio: function(t) {
            var e = t.detail, a = Object.keys(e)[0], i = this.data.form[a];
            this.setData(_defineProperty({}, "form.".concat(a), i === +e[a] ? "" : +e[a]));
        },
        selectMore: function(t) {
            var e = t.detail.option, a = e.filter(function(t) {
                return t.selected;
            });
            this.labelidsName = a.map(function(t) {
                return t.name;
            });
            var i = this.data.form;
            i.labelids = a.map(function(t) {
                return t.id;
            }).join(","), i.type || i.status || i.rate || i.labelids || (i = {}), this.setData({
                form: i,
                "nicheLabel.labels": e
            });
        },
        triggerNiche: function() {
            var e = this, t = this.$appData.card, a = t.guid, i = t.cardid, n = this.data.client;
            if (this.onClose(), !a || !i) return wx.showModal({
                content: "需要创建名片才能转为客户哦",
                success: function(t) {
                    t.confirm && e.$route("/pages/tabbar/card/card", "tab");
                }
            });
            if (this.checkVip()) {
                var r = Promise.resolve();
                "undefined" === n.yetNiche && (r = this.checkClientStatus()), r.then(function() {
                    if (+n.yetNiche === NICHE_STATUS.ALREADY) e.gotoDetail(); else if (+n.yetNiche === NICHE_STATUS.ANOTHER) wx.showModal({
                        content: "该用户已被其他同事转为客户，无法再次转为客户",
                        showCancel: !1,
                        confirmText: "知道了"
                    }); else {
                        e.fetchLabel || e.getLabels();
                        var t = !e.data.showModal;
                        e.setData({
                            showModal: t
                        }), t && e.clearSelectTags();
                    }
                });
            }
        },
        gotoDetail: function() {
            var t = this.$appData.card, e = t.guid, a = t.cardid, i = this.data.client, n = getCurrentPages().slice(-2, -1)[0], r = n && n.route, c = "/pages/radar/nichedetail/nichedetail";
            c.includes(r) ? this.$route("back") : this.$route(c, {
                guid: e,
                cardid: a,
                touid: i.uid
            });
        },
        checkVip: function() {
            return !!this.$appData.card.isFeeVer || (this.$emit("radar.navbar.change", 2), this.$route("/pages/radar/index/index", "tab"), 
            !1);
        },
        clearSelectTags: function() {
            var t = this.data.nicheLabel;
            t && (t.labels.forEach(function(t) {
                t.selected = !1;
            }), this.labelidsName = [], this.setData({
                form: {},
                nicheLabel: t
            }), this.content = "");
        },
        comParams: function() {
            var t = this.data, e = t.client.uid, a = t.form, i = this.$appData.card, n = i.guid;
            return _objectSpread({}, a, {
                cardid: i.cardid,
                guid: n,
                touid: e,
                rate: a.rate ? 10 * a.rate : 0
            });
        },
        relateLalbel: function() {
            var t = this.$appData.card, e = t.guid, a = t.cardid, i = this.data, n = i.client.uid, r = i.form;
            return r.labelids ? _api.default.minicard.businesslabel.relate({
                data: {
                    op: 1,
                    labelid: r.labelids,
                    cardid: a,
                    guid: e,
                    touid: n
                }
            }) : Promise.resolve();
        },
        intoNiche: function() {
            var t = this;
            this.verify() && (this.setData({
                showModal: !1
            }), this.relateLalbel().then(function() {
                _api.default.minicard.business.create({
                    loading: "转入中",
                    data: t.comParams()
                }).then(function() {
                    wx.showToast({
                        title: "已转入客户"
                    }), t.setData({
                        "client.yetNiche": NICHE_STATUS.ALREADY
                    }), t.gotoDetail(), t.$emit("niche.client.change", NICHE_STATUS.ALREADY), t.$emit("niche.list.change"), 
                    t.addRecord();
                });
            }));
        },
        verify: function() {
            var t = this, e = this.data.form, a = [ "type", "status", "rate" ].filter(function(t) {
                return !e[t];
            })[0];
            return !a || (this.setData({
                labelToptip: "点击选择" + NICHE_PARAMS[a]
            }), wx.setTimeout(function() {
                t.setData({
                    labelToptip: ""
                });
            }, 1e3), !1);
        },
        handleRecord: function() {
            var t = this.data, i = t.form, n = t.nicheLabel, e = t.client.realname, a = function(e) {
                var a = "";
                return n[e].forEach(function(t) {
                    +t.id == +i["labels" === e ? "labelids" : e] && (a = t.name);
                }), a;
            }, r = function(t) {
                return "<style color: #09bb07;>[".concat(t, "]</style>");
            }, c = "将".concat(e, "转为客户"), s = c;
            if (i) {
                var o = i.type ? "，类型为".concat(r(a("type"))) : "", d = i.status ? "，状态为".concat(r(a("status"))) : "", l = i.rate ? "，预计成交率为".concat(r(a("rate"))) : "", u = "", h = this.labelidsName || [];
                if (i.labelids && 0 < h.length) {
                    var f = "";
                    h.forEach(function(t, e) {
                        f += "".concat(0 === e ? "" : "，").concat(r(t));
                    }), u = "，添加自定义标签".concat(f);
                }
                s = c + o + d + l + u + "。";
            }
            return s;
        },
        addRecord: function() {
            var t = this.$appData.card, e = t.guid, a = t.cardid, i = this.data.client.uid;
            _api.default.minicard.business.addfollow({
                data: {
                    content: this.handleRecord(),
                    cardid: a,
                    guid: e,
                    touid: i,
                    issystem: 1
                }
            });
        }
    }
});